package com.jerry.passenger.order.model;

import com.google.gson.Gson;
import com.jerry.passenger.MyApplication;
import com.jerry.passenger.account.bean.User;
import com.jerry.passenger.common.databus.RxBus;
import com.jerry.passenger.common.http.IHttpClient;
import com.jerry.passenger.common.http.IRequest;
import com.jerry.passenger.common.http.IResponse;
import com.jerry.passenger.common.http.api.API;
import com.jerry.passenger.common.http.biz.BaseBizResponse;
import com.jerry.passenger.common.http.impl.BaseRequest;
import com.jerry.passenger.common.storage.SPDao;
import com.jerry.passenger.common.util.LogUtil;
import com.jerry.passenger.order.model.response.OrderStateOptResponse;

import rx.functions.Func1;

public class OrderManagerImpl implements IOrderManager {
    private static final String TAG = "OrderMangerImpl";

    IHttpClient mHttpClient;

    public OrderManagerImpl(IHttpClient mHttpClient) {
        this.mHttpClient = mHttpClient;
    }

    /**
     * 取消订单
     * @param orderId
     */
    @Override
    public void cancel(final String orderId) {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                IRequest request = new BaseRequest(API.Config.getDomain() + API.CANCEL_ORDER);
                request.setBody("id", orderId);

                IResponse response = mHttpClient.post(request, false);
                OrderStateOptResponse orderStateOptResponse = new OrderStateOptResponse();
                orderStateOptResponse.setCode(response.getCode());
                orderStateOptResponse.setState(OrderStateOptResponse.ORDER_STATE_CANCEL);
                LogUtil.d(TAG, "cancel order: " + response.getData());

                return orderStateOptResponse;
            }
        });
    }

    /**
     * 支付
     * @param orderId
     */
    @Override
    public void pay(final String orderId) {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                IRequest request = new BaseRequest(API.Config.getDomain() + API.PAY);
                request.setBody("id", orderId);

                IResponse response = mHttpClient.post(request, false);
                OrderStateOptResponse orderStateOptResponse = new OrderStateOptResponse();
                orderStateOptResponse.setCode(response.getCode());
                orderStateOptResponse.setState(OrderStateOptResponse.PAY);
                LogUtil.d(TAG, "pay order: " + response.getData());

                return orderStateOptResponse;
            }
        });
    }

    /**
     *  获取进行中的订单
     */
    @Override
    public void getProcessingOrder() {
        RxBus.getInstance().chainProcess(new Func1() {
            @Override
            public Object call(Object o) {
                SPDao spDao = new SPDao(MyApplication.getInstance(), SPDao.FILE_ACCOUNT);
                User account = (User) spDao.get(SPDao.KEY_ACCOUNT, User.class);
                String uid = account.getUid();   //获取 uid

                IRequest request = new BaseRequest(API.Config.getDomain() + API.GET_PROCESSING_ORDER);
                request.setBody("uid", uid);
                IResponse response = mHttpClient.get(request, false);
                LogUtil.d(TAG, "getProcessingOrder order: " + response.getData());
                if (response.getCode() == BaseBizResponse.STATE_OK) {
                    /**
                     * 解析订单数据，封装到 OrderStateOptResponse
                     */
                    OrderStateOptResponse orderStateOptResponse = new Gson().fromJson(response.getData(), OrderStateOptResponse.class);
                    if (orderStateOptResponse.getCode() == BaseBizResponse.STATE_OK) {
                        orderStateOptResponse.setState(orderStateOptResponse.getData().getState());
                        LogUtil.d(TAG, "getProcessingOrder order state=" + orderStateOptResponse.getState());
                        return orderStateOptResponse;
                    }
                }
                return null;
            }
        });
    }

}
